package wangwenjun.phase1.flightquery.join;

import lombok.extern.slf4j.Slf4j;
import util.ThreadUtil;
import wangwenjun.phase1.flightquery.FlightQuery;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author ChangLiang
 * @date 2020/5/29
 */
@Slf4j
public class FlightQueryJoinTask extends Thread implements FlightQuery {

    private final String origin;

    private final String destination;

    private final List<String> flightList = new ArrayList<>();

    public FlightQueryJoinTask(String airline, String origin, String destination)
    {
        super("[" + airline + "]");
        this.origin = origin;
        this.destination = destination;
    }

    @Override
    public List<String> get() {
        return this.flightList;
    }

    @Override
    public void run() {
        log.info("{}-query from {} to {}", this.getName(), origin, destination);
        int randomVal = ThreadLocalRandom.current().nextInt(10);
        ThreadUtil.sleepForMilliSeconds(randomVal*1000);
        this.flightList.add(this.getName() + "-" + randomVal);
        log.info("The Fight:{} list query successful, consume:{}", getName(), randomVal);
    }
}
